package com.czy.owner.ui.headline;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.czy.owner.R;
import com.czy.owner.adapter.UptoTodayAdapter;
import com.czy.owner.api.UptoTodayApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.UptoTodayModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.hotshop.GoodsDetailActivity;
import com.czy.owner.utils.glide.GlideUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;

import butterknife.BindView;

/**
 * Created by wangyang on 2017/6/23 0023.
 */

public class UptoTodayActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView easyRecyclerView;
    private UptoTodayAdapter adapter;
    private String title;
    private ImageView imgHeadUptoToday;

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        title = getIntent().getStringExtra("title");
        getToolbarTitle().setText(title);
        easyRecyclerView.setLayoutManager(new LinearLayoutManager(UptoTodayActivity.this));
        easyRecyclerView.addItemDecoration(new RecycleViewDivider(UptoTodayActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        easyRecyclerView.setAdapterWithProgress(adapter = new UptoTodayAdapter(UptoTodayActivity.this));
        easyRecyclerView.setRefreshListener(this);
        onRefresh();

        adapter.addHeader(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                LayoutInflater inflater = LayoutInflater.from(UptoTodayActivity.this);
                View view = inflater.inflate(R.layout.head_upto_toaday, null);
                imgHeadUptoToday = (ImageView) view.findViewById(R.id.img_head_upto_today);
                return view;
            }

            @Override
            public void onBindView(View headerView) {

            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<UptoTodayModel.GoodsListBean>() {
            @Override
            public void onItemClick(View view,int position, UptoTodayModel.GoodsListBean model) {
                Intent intent = new Intent(UptoTodayActivity.this, GoodsDetailActivity.class);
                intent.putExtra("goodsId", model.getGoodsId());
                intent.putExtra("mFromType", Constants.GOODSSOURCE_STORE);
                intent.putExtra("mFromTypeId", "0");
                startActivity(intent);
            }
        });

    }

    @Override
    protected void updateViews() {

    }


    private void UptoTodayData(String title) {
        UptoTodayApi uptoTodayApi = new UptoTodayApi(new HttpOnNextListener<UptoTodayModel>() {
            @Override
            public void onNext(UptoTodayModel model) {
                setDataList(model);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<UptoTodayModel>>() {
                }.getType();
                BaseResultEntity<UptoTodayModel> cacheList = gson.fromJson(cache, type);
                setDataList(cacheList.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                easyRecyclerView.setEmptyView(R.layout.view_empty_common);
                easyRecyclerView.showEmpty();
            }
        }, this);
        uptoTodayApi.setType(title);
        if(UserHelper.getInstance().isLogined()){
            uptoTodayApi.setSession(UserHelper.getInstance().getSessionInfoModel(UptoTodayActivity.this).getSession());
        }else {
            uptoTodayApi.setSession("");
        }
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(uptoTodayApi);
    }

    private void setDataList(final UptoTodayModel model) {
        adapter.addAll(model.getGoodsList());
        if (adapter.getAllData().size() == 0) {
            easyRecyclerView.setEmptyView(R.layout.view_empty_common);
            easyRecyclerView.showEmpty();
        }
        GlideUtils.loadImage(UptoTodayActivity.this, model.getLogo(), imgHeadUptoToday, new GlideUtils.ImageLoadListener<String, GlideDrawable>() {
            @Override
            public void onLoadingComplete(String uri, ImageView view, GlideDrawable resource) {

            }

            @Override
            public void onLoadingError(String source, Exception e) {

            }
        }, R.mipmap.default_banner, R.mipmap.default_banner);
    }

    @Override
    public void onRefresh() {
        //刷新
        adapter.clear();
        UptoTodayData(title);

    }
}
